package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.bizcore.web.utils.EasyPoiStylerColorImpl;
import com.neusoft.neuiotms.dcam.fixed.converter.StockConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.StockDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Stock;
import com.neusoft.neuiotms.dcam.fixed.repository.StockRepository;
import com.neusoft.neuiotms.dcam.fixed.service.StockService;
import com.neusoft.neuiotms.dcam.fixed.vo.StockExportVO;
import com.neusoft.neuiotms.dcam.fixed.vo.StockQueryExportVO;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

@Service
public class StockServiceImpl implements StockService {

    @Autowired
    private StockRepository stockRepository;

    @Autowired
    private StockConverter stockConverter;

    @Override
    public StockDTO create(final StockDTO dto) {
        Stock model = null;
        if (dto != null) {
            model = this.stockConverter.toModel(dto);
            model = this.stockRepository.save(model);
        }
        return this.stockConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        this.stockRepository.deleteById(id);
    }

    @Override
    public StockDTO update(final Long id, final StockDTO dto) {
        Stock model = null;
        try {
            if (dto != null) {
                model = this.stockRepository.getOne(id);

                if (model != null) {
                    dto.setId(model.getId());
                    model = this.stockConverter.toModel(dto);
                    this.stockRepository.save(model);
                }
            }

        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        return this.stockConverter.toDTO(model);
    }

    @Override
    public List<StockDTO> findAll() {
        final List<Stock> models = this.stockRepository.findAll();
        if (CollectionUtils.isNotEmpty(models)) {
            return this.stockConverter.toListDTO(models);
        }
        return null;
    }

    @Override
    public StockDTO details(final Long id) {
        if (id == null) {
            return null;
        }
        final Stock model;
        try {
            model = this.stockRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.stockConverter.toDTO(model);
    }

    @Override
    public Page<StockDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<Stock> result = this.stockRepository.search(pageable, searchable);
        return this.stockConverter.toPageDTO(result);
    }

    @Override
    public Workbook export(final Searchable searchable, final Pageable pageable) {
        final Page<Stock> models = this.stockRepository.search(pageable, searchable);
        final List<StockExportVO> list = new ArrayList<>(models.getSize() == 0 ? 1 : models.getSize());
        models.forEach(model -> {
            list.add(this.stockConverter.toVO(model));
        });
        final ExportParams param = new ExportParams("库存导出", "库存导出", ExcelType.XSSF);
        param.setStyle(EasyPoiStylerColorImpl.class);

        return ExcelExportUtil.exportExcel(param, StockExportVO.class, list);
    }

    @Override
    public Workbook downloadStockQuery(final Searchable searchable, final Pageable pageable) {
        final Page<Stock> models = this.stockRepository.search(pageable, searchable);
        final List<StockQueryExportVO> list = new ArrayList<>(models.getSize() == 0 ? 1 : models.getSize());
        models.forEach(model -> {
            list.add(this.stockConverter.toStockQueryVO(model));
        });
        final ExportParams param = new ExportParams("库存查询结果导出", "库存查询结果导出", ExcelType.XSSF);
        param.setStyle(EasyPoiStylerColorImpl.class);

        return ExcelExportUtil.exportExcel(param, StockQueryExportVO.class, list);
    }

}
